import { login, getUserCheckPwd } from '@/api/api.js'
import router from '@/router/index'
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { useRouterStore } from './router'
import { getSysparamFileServer } from '@/api/api.js'
import { dictStore } from './dict'
import { sessionMemory } from '@/utils/sessionMemory'
export const useUserStore = defineStore('user', () => {
  const token = ref(sessionMemory.getItem('token'))
  const userInfo = ref(sessionMemory.getItem('userInfo'))
  const orgInfo = ref(sessionMemory.getItem('orgInfo'))
  const roleInfo = ref(sessionMemory.getItem('roleInfo'))
  const userAreaArr = ref(sessionMemory.getItem('userAreaArr'))
  const userLastArea = ref(sessionMemory.getItem('userLastArea'))
  const userLastName = ref(sessionMemory.getItem('userLastName'))
  const fileUrl = ref(sessionMemory.getItem('fileUrl'))
  const videoUrl = ref(sessionMemory.getItem('videoUrl'))

  // 存储token
  const setToken = (val) => {
    token.value = val
    sessionMemory.setItem("token", token.value)
  }

  // 存储用户信息
  const setUser = (val) => {
    sessionMemory.setItem("userInfo", val)
    userInfo.value = val
  }

  // 存储用户所属机构信息
  const setOrg = (val) => {
    if (val) {
      sessionMemory.setItem("orgInfo", val)
      orgInfo.value = val // level 1省 2市 3县区 4街道乡镇 5社区村
      // 存储用户区划
      let userAreaArrKey = ['provinceCode', 'cityCode', 'countyCode', 'streetCode', 'communityCode']
      let userAreaArrKey2 = ['provinceName', 'cityName', 'countyName', 'streetName', 'communityName']
      let areaArr = []
      let areaArr2 = []
      userAreaArrKey.forEach(item => {
        val[item] && areaArr.push(val[item])
      })
      userAreaArrKey2.forEach(item => {
        val[item] && areaArr2.push(val[item])
      })
      userAreaArr.value = areaArr
      let arr = areaArr.filter(item => item)
      let arr2 = areaArr2.filter(item => item)
      userLastArea.value = arr[arr.length - 1]
      userLastName.value = arr2[arr2.length - 1]
      sessionMemory.setItem('userAreaArr', areaArr)
      sessionMemory.setItem('userLastArea', arr[arr.length - 1])
      sessionMemory.setItem('userLastName', arr2[arr2.length - 1])
    }
  }

  // 存储用户所属角色信息
  const setRole = (val) => {
    roleInfo.value = val
    sessionMemory.setItem("roleInfo", val)
  }

  const setFileUrl = async () => {
    const res = await getSysparamFileServer()
    if (res.succeed) {
      fileUrl.value = location.origin + '/api' + res.data.fileServer
      videoUrl.value = res.data.videoM3u8Server
      sessionMemory.setItem("fileUrl", fileUrl.value)
      sessionMemory.setItem("videoUrl", videoUrl.value)
      return true
    }
  }

  /* 登录 */
  const LoginIn = async (loginInfo) => {
    try {
      const result = await login(loginInfo)
      if (result.succeed) {
        //存储密钥（加密）
        setToken(result.data.accessToken)
        setOrg(result.data.org)
        setRole(result.data.roleList)
        // 存储用户信息
        setUser(result.data.user)
        // 存储fileUrl
        await setFileUrl()
        // 存储字典
        await dictStore().getDict()
      }
      return { ...result }
    } catch (e) {
      console.log(e);
    }
  }

  /* 清理数据 */
  const ClearStorage = () => {
    dictStore().clearDict()
  }

  /* 登出*/
  const LoginOut = () => {
    ClearStorage()
    router.push({ name: 'login', replace: true })
  }

  return {
    fileUrl,
    videoUrl,
    userInfo,
    userLastArea,
    userLastName,
    userAreaArr,
    orgInfo,
    roleInfo,
    token,
    LoginIn,
    setUser,
    LoginOut,
    setToken,
    setFileUrl,
    ClearStorage,
  }
})
